package com.ruoyi.project.cms.sa.order.type.controller;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.sa.order.type.domain.Type;
import com.ruoyi.project.cms.sa.order.type.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-17
 */
@Controller
@RequestMapping("/cms/sa/order/type")
public class TypeController extends BaseController {

    // 当前控制类的请求地址前缀
    private String prefix = "cms/sa/order/type";

    @Autowired
    private ITypeService typeService;

    /**
     * 转发到 type.html 页面
     * @return
     */
    @GetMapping()
    public String type() {
        return prefix + "/type";
    }

    /**
     * 根据条件分页查询订单类别数据
     * @param type
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Type type) {
        startPage();
        List<Type> list = typeService.selectTypeList(type);
        return getDataTable(list);
    }

    /**
     * 新增订单类别
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单类别
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Type type) {
        if (CMSConstants.ORDER_TYPE_NAME_NOT_UNIQUE.equals(typeService.checkTypeNameUnique(type))) {
            return error("新增类别'" + type.getTypeName() + "'失败，类别名称已存在");
        }
        return toAjax(typeService.insertType(type));
    }

    /**
     * 批量删除订单类别
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(typeService.deleteTypeByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改订单类别信息
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Integer typeId, ModelMap mmap) {
        // 根据类别ID查询订单类别信息
        mmap.put("type", typeService.selectTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存订单类别信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Type type) {
        if (CMSConstants.ORDER_TYPE_NAME_NOT_UNIQUE.equals(typeService.checkTypeNameUnique(type))) {
            return error("修改类别'" + type.getTypeName() + "'失败，类别名称已存在");
        }
        return toAjax(typeService.updateType(type));
    }

    /**
     * 查看订单类别详情
     *
     * @param typeId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{typeId}")
    public String detail(@PathVariable("typeId") Integer typeId, ModelMap mmap) {
        mmap.put("name", "type");
        mmap.put("type", typeService.selectTypeById(typeId));
        return prefix + "/detail";
    }

    /**
     * 校验订单类别名称是否唯一
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(Type type) {
        return typeService.checkTypeNameUnique(type);
    }
}
